﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace AnalyzeCluterResults
{
    class Program
    {

        static void Main(string[] args)
        {
            Dictionary<string, string> domainIPMap = new Dictionary<string, string>();
            Dictionary<string, string> domainURLMap = new Dictionary<string, string>();
            StreamReader reader = new StreamReader(@"D:\Pop Team Work\Bing Job Output\Src-Src Pair Similarity Analysis\CurshList_target500_above08_withIP_Partial.txt");
            string line = null;
            while ((line = reader.ReadLine()) != null)
            {
                string[] ss = line.Split('\t');
                if(ss.Length == 4)
                {
                    try
                    {
                        domainIPMap.Add(ss[0], ss[3]);
                    }
                    catch (Exception)
                    {
                        Console.WriteLine(ss[0]);
                    }
                }
            }
            reader.Close();
            reader = new StreamReader(@"D:\Pop Team Work\Bing Job Output\Src-Src Pair Similarity Analysis\CurshList_652cf3ba-7c7e-41d7-a9ca-fad3eb3189b5_withURL.txt");
            line = null;
            while ((line = reader.ReadLine()) != null)
            {
                string[] ss = line.Split('\t');
                if (ss.Length == 2)
                {
                    try
                    {
                        domainURLMap.Add(ss[1], ss[0]);
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            reader.Close();

            reader = new StreamReader(@"D:\Pop Team Work\Bing Job Output\Src-Src Pair Similarity Analysis\Clustering Results\clusters_500_srcsrcpair_similarity_above8_full.txt");
            line = null;
            int count = 0;
            StreamWriter writer = File.CreateText(@"D:\Pop Team Work\Bing Job Output\Src-Src Pair Similarity Analysis\Clustering Results\clusters_500_similarity_above8_full_analysis.txt");
            StreamWriter writer_ip = File.CreateText(@"D:\Pop Team Work\Bing Job Output\Src-Src Pair Similarity Analysis\Clustering Results\clusters_500_similarity_above8_full_analysis_ip_distribution.txt");
            while ((line = reader.ReadLine()) != null)
            {
                string[] ss = line.Split('\t');
                if (ss.Length == 2)
                {
                    int cluterSize = Convert.ToInt32(ss[0]);
                    int clusterID = ++count;
                    List<ClusterElement> list = getClusterEles(ss[1]);
                    int domainHavingIP = 0;
                    HashSet<string> differentIPs = new HashSet<string>();
                    foreach (ClusterElement ele in list)
                    {
                        string url = "";
                        string ip = "";
                        try
                        {
                            url = domainURLMap[ele.domainHash];
                        }
                        catch (Exception)
                        {
                        }
                        try
                        {
                            ip = domainIPMap[ele.domainHash];
                            domainHavingIP++;
                            differentIPs.Add(ip);
                        }
                        catch (Exception)
                        {
                        }

                        writer.WriteLine(clusterID + "\t" + cluterSize + "\t" + ele.domainHash + "\t"+ ele.outlinks+ "\t" + url + "\t" + ip);
                        Console.WriteLine(clusterID + "\t" + cluterSize + "\t" + ele.domainHash + "\t" + ele.outlinks + "\t" + url + "\t" + ip);
                    }
                    if ((domainHavingIP / (double)cluterSize) > 0.5)
                    {
                        writer_ip.WriteLine(clusterID + "\t" + cluterSize + "\t" + domainHavingIP + "\t" + differentIPs.Count);
                    }
                }
            }
            reader.Close();
            writer.Close();
            writer_ip.Close();
        }

        static List<ClusterElement> getClusterEles(string str)
        {
            List<ClusterElement> list = new List<ClusterElement>();
            string[] ss = str.Split(',');
            foreach (string elestr in ss)
            {
                string[] sss = elestr.Split('-');
                if (sss.Length == 4)
                {
                    ClusterElement ele = new ClusterElement();
                    ele.domainHash = sss[0];
                    ele.outlinks = Convert.ToInt32(sss[2]);
                    list.Add(ele);
                }
            }
            return list;
        }
    }

    class ClusterElement
    {
        public string domainHash;
        public int outlinks;
        int clusterID;
        int cluserSize;
    }
}
